package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.dto.OrdersDto;
import com.example.entity.OrderDetail;
import com.example.entity.Orders;
import com.example.service.OrderDetailService;
import com.example.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.beans.beancontext.BeanContext;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    //添加下单后信息到数据库
    @PostMapping("/submit")
    public R<String>  submit(@RequestBody Orders orders){
        log.info("订单信息：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }
    //历史订单
//    @GetMapping("/userPage")
//    public R<Page> userPage(int page, int pageSize){
//        log.info("page:{},pagesize:{}",page,pageSize);
//        Page pageInfo = new Page(page,pageSize);
//        //条件排序
//        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
//        lqw.orderByDesc(Orders::getOrderTime);
//
//        ordersService.page(pageInfo,lqw);
//        return R.success(pageInfo);
//    }
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize, HttpSession session){
        log.info("page:{},pagesize:{}",page,pageSize);
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage=new Page<>();
        //条件排序
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        Long userId=(Long) session.getAttribute("user");
        lqw.eq(Orders::getUserId,userId);
        lqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,lqw);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list  = records.stream().map((item) -> {
            OrdersDto ordersDto=new OrdersDto();

            BeanUtils.copyProperties(item,ordersDto);

            Long orderId = item.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderDetail::getOrderId,orderId);
            //根据id查询对应的菜品或套餐
            List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);
            if(orderDetails!=null){
                ordersDto.setOrderDetails(orderDetails);
            }
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }
    //后台订单明细
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number){
        log.info("获取的数据：{},{}",page,pageSize);
        log.info("订单号为：{}",number);
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.like(number!=null,Orders::getNumber,number);
        //排序
        lqw.orderByDesc(Orders::getOrderTime);

        Page<Orders> ordersPage = ordersService.page(pageInfo,lqw);

        return R.success(ordersPage);
    }

    //后天订单状态
    @PutMapping
    public R<String> status(@RequestBody Orders orders){
        log.info("获取的数据为：{}",orders.toString());

        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Orders::getNumber,orders.getNumber());

        ordersService.update(orders,lqw);

        return R.success("状态修改成功");
    }
}
